<?php
namespace app\tools\logic;
use think\facade\Db;

class ChannelCurrencyLogic{
	static function getList(){
		$page_size = input('page_size', 10);
		$currency_name = input('currency_name', '');

		$obj = Db::name('channel_currency')
				->alias('c')
				->field('c.*')
				->where('c.status','<>',127);
		$currency_name && $obj->where('currency_name','like','%'.$currency_name.'%');
		$list = $obj->order('c.id desc')
				->paginate($page_size);
		return $list;
	}

	static function getInfo(){
		$id = input('id');
		if(!$id){
			return '';
		}
		$info = Db::name('channel_currency')
				->alias('c')
				->field('c.*')
				->where('c.status','<>',127)
				->where('c.id', $id)
				->find();
		return $info;

	}

	static function save(){
		$id = input('id');
		if(!$id){
			return 110;
		}
		$data = [];
		$data['status'] = input('status', 1);
		$data['status'] = input('status', 1);
		$data['currency_name'] = input('currency_name', "");
		$data['currency_symbol'] = input('currency_symbol', '');
		$data['currency_code'] = input('currency_code','');
		$data['exchange_rate'] = input('exchange_rate', 0);

		$info = Db::name('channel_currency')
					->field('id')
					->where('currency_code', $data['currency_code'])
					->where('status','<>',127)
					->where('id','<>', $id)
					->find();
		if($info){
			return 133;
		}

		Db::name('channel_currency')->where('id', $id)->update($data);
		return 0;
	}

	static function add(){
		$data = [];
		$data['status'] = input('status', 1);
		$data['currency_name'] = input('currency_name', "");
		$data['currency_symbol'] = input('currency_symbol', '');
		$data['exchange_rate'] = input('exchange_rate', 0);
		$data['currency_code'] = input('currency_code','');
		$data['create_time'] = date('Y-m-d H:i:s');

		$info = Db::name('channel_currency')
					->field('id')
					->where('currency_code', $data['currency_code'])
					->where('status','<>',127)
					->find();
		if($info){
			return 132;
		}

		Db::name('channel_currency')->insertGetId($data);
		return 0;
	}

	//删除
	static function delete(){
		$id = input('id','');
		$ids = explode(',', $id);
		if(!$ids || !$id){
			return 110;
		}

		$info = Db::name('channel')
				->where('channel_currency_id','in',$ids)
				->where('status',1)
				->find();
		if($info){
			return 141;
		}

		$rs = Db::name('channel_currency')
				->where('id','in', $ids)
				->update(['status'=>127]);
		if($rs!==false){
			return 0;
		}else{
			return 100000;
		}
	}
}